"""
GameConfig with registered default values
"""

import mjin2

class GameConfig(object):
    def __init__(self):
        self.cfg = mjin2.ConfigFile()
        self.defaults = {}
    def __del__(self):
        del self.cfg
    def get(self, key):
        if (not self.__exists(key)):
            raise Exception("GameConfig has no {0} key. Forgot to reg()ister it?".format(key))
        return self.cfg.get(key)
    def load(self, fileName):
        self.cfg.loadAndCreate(fileName)
    def reg(self, key, defaultValue):
        self.defaults[key] = defaultValue
        self.cfg.set(key, defaultValue)
    def reset(self, key = None):
        # Key provided.
        if (key):
            self.cfg.set(key, self.defaults[key])
            return
        # No key provided.
        for key in self.defaults:
            self.cfg.set(key, self.defaults[key])
    def save(self, fileName):
        self.cfg.save(fileName)
    def set(self, key, value):
        if (not self.__exists(key)):
            raise Exception("GameConfig has no {0} key. Forgot to reg()ister it?".format(key))
        self.cfg.set(key, value)
    def windowConfig(self):
        wc = mjin2.WindowConfig()
        if (self.__exists("AntiAliasing")):
            wc.antiAliasing = int(self.cfg.get("AntiAliasing"))
        if (self.__exists("FPS")):
            wc.fps = int(self.cfg.get("FPS"))
        if (self.__exists("FullScreen")):
            wc.fullScreen = True if (self.cfg.get("FullScreen") == "1") else False
        if (self.__exists("Resolution")):
            (width, height) = self.cfg.get("Resolution").split(" x ")
            wc.width = int(width)
            wc.height = int(height)
        if (self.__exists("VSync")):
            wc.vsync = True if (self.cfg.get("VSync") == "1") else False
        return wc
    def __exists(self, key):
        return (key in self.defaults)

